#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->pushButton_false->setEnabled(false);

    //设置被移动的图片和大小
    ui->pushButton_people->setIcon(QIcon(":/people.png"));
    ui->pushButton_people->setIconSize(QSize(500, 500));
    //设置方向键的图标
    ui->pushButton_up->setIcon(QIcon(":/up.png"));
    ui->pushButton_down->setIcon(QIcon(":/down.png"));
    ui->pushButton_left->setIcon(QIcon(":/left.png"));
    ui->pushButton_right->setIcon(QIcon(":/right.png"));

    //设置快捷键--方式1
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));
    //设置快捷键--方式2
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));

    //设置鼠标连续触发（对于键盘来说默认就是连发的）
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}

//向上移动的按钮
void Widget::on_pushButton_up_clicked()
{
    //首先获取到人物的位置
    QRect rect = ui->pushButton_people->geometry();
    //基于上次的位置设置新的位置
    ui->pushButton_people->setGeometry(rect.x(), rect.y() - 5, rect.width(), rect.height());

}

//向下移动的按钮
void Widget::on_pushButton_down_clicked()
{
    //首先获取到人物的位置
    QRect rect = ui->pushButton_people->geometry();
    //基于上次的位置设置新的位置
    ui->pushButton_people->setGeometry(rect.x(), rect.y() + 5, rect.width(), rect.height());
}

//向左移动的按钮
void Widget::on_pushButton_left_clicked()
{
    //首先获取到人物的位置
    QRect rect = ui->pushButton_people->geometry();
    //基于上次的位置设置新的位置
    ui->pushButton_people->setGeometry(rect.x() - 5, rect.y(), rect.width(), rect.height());
}

//向右移动的按钮
void Widget::on_pushButton_right_clicked()
{
    //首先获取到人物的位置
    QRect rect = ui->pushButton_people->geometry();
    //基于上次的位置设置新的位置
    ui->pushButton_people->setGeometry(rect.x() + 5, rect.y(), rect.width(), rect.height());
}
